package com.study.ming.websocket;

import com.study.ming.entity.WeatherPojo;
import com.study.ming.feign.WeatherFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author yh
 * @date 2022/10/12 12:08
 **/
@RestController
public class HomeController {
    @Autowired
    private DiscoveryClient discoveryClient;

//    @Autowired
    WeatherFeign weatherFeign;
//    @Autowired
//    private RestTemplate restTemplate;
//    @Autowired
//    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/getByCity")
    public WeatherPojo getWeatherByCity(@RequestParam(name="id", value = "id", required = false, defaultValue = "1") Integer id){
        List<String> services = discoveryClient.getServices();
        System.out.println(services);
//        List<ServiceInstance> weather = discoveryClient.getInstances("weather");
//        ServiceInstance instance = weather.get(0);
//        System.out.println(instance);
        return weatherFeign.getWeatherByCity(id);
    }
//    @GetMapping("/getByCity2")
//    public WeatherPojo getWeatherByCity2(@RequestParam(name="id", value = "id", required = false, defaultValue = "1") Integer id){
//        return restTemplate.getForObject("http://weather/getByCity?id="+id, WeatherPojo.class);
//    }
//    @GetMapping("/getByCity3")
//    public WeatherPojo getWeatherByCity3(@RequestParam(name="id", value = "id", required = false, defaultValue = "1") Integer id){
//        //使用 LoadBalanceClient 和 RestTemolate 结合的方式来访问
//        ServiceInstance serviceInstance = loadBalancerClient.choose("weather");
//        String url = String.format("http://%s:%s/getByCity?id=%s",serviceInstance.getServiceId(),serviceInstance.getPort(),id);
//        System.out.println("request url:"+url);
//        return restTemplate.getForObject(url,WeatherPojo.class);
//    }
}
